package com.shuqi.activity.viewport;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuqi.common.RedDotProvider;
import com.shuqi.controller.R;
import com.shuqi.database.dao.impl.VersionShowDao.VersionShowEnum;

/**
 * 
 * @author li.li
 * @date 2014-6-13
 * 
 *       书架标题
 * 
 */
public class BookShelfTitle extends LinearLayout {

    private View view;
    private TextView titleTextView;
    public ImageView leftImageView;
    private ImageView titleLeftRedPoint;
    private ImageView rightImageView;

    public BookShelfTitle(Context context) {
        super(context);
    }

    public BookShelfTitle(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.bookshelf_title, this);
        titleTextView = (TextView) view.findViewById(R.id.title_text);
        leftImageView = (ImageView) view.findViewById(R.id.title_left);
        rightImageView = (ImageView) view.findViewById(R.id.title_right);
        titleLeftRedPoint = (ImageView) view.findViewById(R.id.title_left_red_point);

        TypedArray typeArray = context.obtainStyledAttributes(attributeSet, R.styleable.title);
        String strTitle = typeArray.getString(R.styleable.title_titleText);
        Drawable imgLeft = typeArray.getDrawable(R.styleable.title_leftbg);
        Drawable imgRight = typeArray.getDrawable(R.styleable.title_rightbg);

        titleTextView.setText(strTitle);
        leftImageView.setImageDrawable(imgLeft);
        rightImageView.setImageDrawable(imgRight);

    }

    public void setTitle(String strTitle) {
        titleTextView.setText(strTitle);
    }

    public void setLeftImage(int imageId) {
        leftImageView.setImageDrawable(getResources().getDrawable(imageId));
    }

    public void setRightImage(int imageId) {}

    public void setStyle(boolean leftVisible, boolean rightVisible) {
        leftImageView.setVisibility(leftVisible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setImageListener(OnClickListener imageListener) {
        leftImageView.setOnClickListener(imageListener);
        rightImageView.setOnClickListener(imageListener);
    }

    public void setLeftClickListener(OnClickListener leftListener) {
        if (leftListener != null) {
            leftImageView.setOnClickListener(leftListener);
        }
    }

    public void setRightClickListener(OnClickListener rightListener) {
        if (rightListener != null) {
            rightImageView.setOnClickListener(rightListener);
        }
    }

    public void handleBookShelfMoreRedDot() {

        boolean isShowRedDot =
                RedDotProvider.getInstance().isShowRedDot(VersionShowEnum.bookshelf_more_red_dot);

        if (isShowRedDot)
            titleLeftRedPoint.setVisibility(View.VISIBLE);
        else
            hideRedPoint();
    }

    /**
     * @return the leftImageView
     */
    public ImageView getLeftImageView() {
        return leftImageView;
    }

    public void hideRedPoint() {
        if (titleLeftRedPoint != null) {
            titleLeftRedPoint.setVisibility(View.GONE);
        }
    }

    public void hideRight() {
        if (rightImageView != null) {
            rightImageView.setVisibility(View.GONE);
        }
    }

    public void showRight() {
        if (rightImageView != null) {
            rightImageView.setVisibility(View.VISIBLE);
        }
    }
}
